package org.example.generics;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class Demo4 {
    public static void main(String[] args) {
        List<String> list = new ArrayList<>();
        Class<?> clazz = list.getClass();
        System.out.println(clazz);  // class java.util.ArrayList
        Type type = clazz.getGenericSuperclass();
        System.out.println(type);	// class java.util.ArrayList<E>

        Box<String> box = new Box<>("asd");
        Class<?> contentType = box.getContentType();
        System.out.println(contentType);    // class java.lang.String
    }

    public static class Box<T> {
        private T content;
        public Box(T content){
            this.content = content;
        }
        public T getContent(){
            return content;
        }
        public void setContent(T content){
            this.content = content;
        }
        // 用于返回泛型类型参数的实际字节码类型，可以间接地获取到泛型类型的信息了
        public Class<?> getContentType() {
            return content.getClass();
        }
    }
}
